package com.ruoyi.hqfinance.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.GetDateStringUtils;
import com.ruoyi.hqfinance.domain.HqBillCollect;
import com.ruoyi.hqfinance.domain.HqBillData;
import com.ruoyi.hqfinance.domain.HqBillDataShow;
import com.ruoyi.hqfinance.domain.HqBillNearlySevenDay;
import com.ruoyi.hqfinance.domain.vo.HqBillCollectExportVo;
import com.ruoyi.hqfinance.domain.vo.HqBillCollectVo;
import com.ruoyi.hqfinance.domain.vo.HqBillDataShowVo;
import com.ruoyi.hqfinance.mapper.HqBillCollectMapper;
import com.ruoyi.hqfinance.mapper.HqBillDataShowMapper;
import com.ruoyi.hqfinance.mapper.HqBillNearlySevenDayMapper;
import com.ruoyi.hqfinance.service.HqBillCollectService;
import com.ruoyi.logistic.domain.HqBillCollectDetail;
import com.ruoyi.logistic.mapper.HqBillCollectDetailMapper;
import com.ruoyi.statisticalCenter.utils.BigDecimalUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class HqBillCollectServiceImpl implements HqBillCollectService {
    @Autowired
    private HqBillCollectMapper hqBillCollectMapper;
    @Autowired
    private HqBillDataShowMapper hqBillDataShowMapper;
    @Autowired
    private HqBillNearlySevenDayMapper hqBillNearlySevenDayMapper;
    @Autowired
    private HqBillCollectDetailMapper hqBillCollectDetailMapper;

    @Override
    public List<HqBillCollect> collect(HqBillCollectVo hqBillCollectVo) {
        PageHelper.startPage(hqBillCollectVo.getPageDate());
        List<HqBillCollect> billCollectList = hqBillCollectMapper.collect(hqBillCollectVo);
        for (HqBillCollect hqBillCollect : billCollectList) {
            if (hqBillCollect != null) {
                hqBillCollect.setCollectDate(hqBillCollectVo.getStartTime() + "至" + hqBillCollectVo.getEndTime());
            }
        }
        return billCollectList;
    }

    @Override
    public List<HqBillCollect> collectExport(HqBillCollectExportVo vo, OutputStream outputStream) {
        List<HqBillCollect> billCollectList = hqBillCollectMapper.collectExport(vo);
        for (HqBillCollect hqBillCollect : billCollectList) {
            if (hqBillCollect != null) {
                hqBillCollect.setCollectDate(vo.getStartTime() + "至" + vo.getEndTime());
            }
        }
        Workbook workbook = new XSSFWorkbook(); // 创建一个新的Excel工作簿
        Sheet sheet = workbook.createSheet("Amount Data"); // 创建一个新的工作表
// 创建边框样式
        CellStyle borderStyle = workbook.createCellStyle();
        BorderStyle thin = BorderStyle.THIN;
        short blackColorIndex = IndexedColors.BLACK.getIndex();
        borderStyle.setBorderTop(thin);
        borderStyle.setBorderBottom(thin);
        borderStyle.setBorderLeft(thin);
        borderStyle.setBorderRight(thin);
        borderStyle.setTopBorderColor(blackColorIndex);
        borderStyle.setBottomBorderColor(blackColorIndex);
        borderStyle.setLeftBorderColor(blackColorIndex);
        borderStyle.setRightBorderColor(blackColorIndex);

// 创建表头样式
        CellStyle headerStyle = workbook.createCellStyle();
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerFont.setFontHeightInPoints((short) 14); // 设置字体大小
        headerStyle.setFont(headerFont);
        headerStyle.setAlignment(HorizontalAlignment.CENTER); // 设置水平居中
        headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 设置垂直居中
        headerStyle.setBorderTop(thin);
        headerStyle.setBorderBottom(thin);
        headerStyle.setBorderLeft(thin);
        headerStyle.setBorderRight(thin);
        headerStyle.setTopBorderColor(blackColorIndex);
        headerStyle.setBottomBorderColor(blackColorIndex);
        headerStyle.setLeftBorderColor(blackColorIndex);
        headerStyle.setRightBorderColor(blackColorIndex);
        Row headerRow = sheet.createRow(0); // 创建表头行
        String[] headers = {"统计日期","承包区","费用大类","收入","支出","合计金额","结算网点","客户"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(headerStyle);
        }

        int rowNum = 1; // 行索引从1开始（因为已经有表头行）

        for (HqBillCollect data : billCollectList) {
            Row row = sheet.createRow(rowNum++);
            Cell cell = row.createCell(0);
            cell.setCellValue(data.getCollectDate());
            cell.setCellStyle(borderStyle);
            
            Cell cell1 = row.createCell(1);
            cell1.setCellValue(data.getSettleNetworkNameId());
            cell1.setCellStyle(borderStyle);

            Cell cell2 = row.createCell(2);
            cell2.setCellValue(data.getCostBigKindId());
            cell2.setCellStyle(borderStyle);

            // 设置金额字段为数值型
            Cell incomeCell = row.createCell(3);
            incomeCell.setCellValue(data.getCollectIncome());
            incomeCell.setCellType(CellType.NUMERIC);
            incomeCell.setCellStyle(borderStyle);

            Cell expendCell = row.createCell(4);
            expendCell.setCellValue(data.getCollectExpend());
            expendCell.setCellType(CellType.NUMERIC);
            expendCell.setCellStyle(borderStyle);

            Cell amountCell = row.createCell(5);
            amountCell.setCellValue(data.getCollectAmount());
            amountCell.setCellType(CellType.NUMERIC);
            amountCell.setCellStyle(borderStyle);

            Cell cell6 = row.createCell(6);
            cell6.setCellValue(data.getSettleNetworkName());
            cell6.setCellStyle(borderStyle);

            Cell cell7 = row.createCell(7);
            cell7.setCellValue(data.getNetworkClient());
            cell7.setCellStyle(borderStyle);
        }

        try {
            workbook.write(outputStream); // 将工作簿写入输出流
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            workbook.close(); // 关闭工作簿
        } catch (IOException e) {
            e.printStackTrace();
        }

        return billCollectList;
    }

    @Override
    public List<HqBillDataShow> billDataShow(HqBillDataShowVo hqBillDataShowVo) {
        List<HqBillDataShow> dataShowList = hqBillDataShowMapper.billDataShow(hqBillDataShowVo);
//        for (HqBillDataShow billDataShow : dataShowList){
//            if (hqBillDataShowVo.getCollectType() == 1){
//                billDataShow.setLoopRatio(hqBillDataShowMapper.yestLoopRatio());
//            }
//        }
        return dataShowList;
    }

    @Override
    public List<HqBillNearlySevenDay> nearlySevenDay(String costBigKind) {
        return hqBillNearlySevenDayMapper.nearlySevenDay(costBigKind);
    }

    @Override
    public HqBillData data(HqBillDataShowVo vo) {
        HqBillData data = new HqBillData();
        data.setCard(hqBillDataShowMapper.billDataShow(vo));
        data.setSeven(hqBillNearlySevenDayMapper.nearlySevenDay(vo.getSevenMark()));
        return data;
    }

    @Override
    public void setCache() {
        String[] costBigKindIds= {/*"","奖惩","面单","派费","中转","总部市场","综合",*/"运输"};
        Integer[] types = {1,2,3};
        Integer[] dateTypes = {0,1};
        List<String> times = GetDateStringUtils.generateDatesForMonth(2024, 5);
        
        //for (String time : times) {
        String time = "20240501";
            for (String costBigKindId : costBigKindIds) {
                for (Integer type : types) {
                    for (Integer dateType : dateTypes) {
                        List<HqBillCollectDetail> list = new ArrayList<>();
                        HqBillCollectVo hqBillCollectVo = new HqBillCollectVo();
                        hqBillCollectVo.setStartTime(time);
                        hqBillCollectVo.setEndTime(time);
                        hqBillCollectVo.setType(type);
                        hqBillCollectVo.setCostBigKindId(costBigKindId);
                        hqBillCollectVo.setDateType(dateType);

                        String startTime = hqBillCollectVo.getStartTime().replace("-", "");
                        String endTime = hqBillCollectVo.getEndTime().replace("-", "");
                        hqBillCollectVo.setStartTime(startTime);
                        hqBillCollectVo.setEndTime(endTime);
                        List<HqBillCollect> billCollectList = hqBillCollectMapper.collect(hqBillCollectVo);
                        for (HqBillCollect hqBillCollect : billCollectList) {
                            HqBillCollectDetail hqBillCollectDetail = new HqBillCollectDetail();
                            BeanUtils.copyProperties(hqBillCollect,hqBillCollectDetail);
                            hqBillCollectDetail.setCollectDate(time);
                            hqBillCollectDetail.setType(String.valueOf(type));
                            hqBillCollectDetail.setDateType(String.valueOf(dateType));
                            
                            list.add(hqBillCollectDetail);
                        }
                        System.out.println("当前时间:"+time);
                        System.out.println("费用大类:"+costBigKindId);
                        System.out.println("网点层级:"+type);
                        if (list.size()>0) {
                            int i = hqBillCollectDetailMapper.insertList(list);
                            System.out.println(time+"插入了"+i+"条数据");
                        }
                        
                        
                    }
                }
            }
            
        //}
    }
}
